package com.example.covid19.controller;


import cn.hutool.json.JSONString;
import cn.hutool.json.JSONUtil;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.StationMessage;
import com.example.covid19.entity.Student;
import com.example.covid19.service.StationMessageService;
import com.example.covid19.service.StudentService;
import com.example.covid19.util.RabbitMqUtil;
import com.google.gson.Gson;
import com.rabbitmq.client.AMQP;
import org.apache.commons.lang.SerializationUtils;
import org.junit.Test;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * <p>
 * VIEW 前端控制器
 * </p>
 *
 * @author BaiZhengChun
 * @since 2023-04-06
 */
@RestController
@RequestMapping("/room-amount")
public class RoomAmountController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    RabbitMqUtil rabbitMqUtil;
    @Autowired
    StudentService studentService;
    @Autowired
    StationMessageService stationMessageService;
    @RequestMapping("/test")
    public void sts(){
        Map<String, Object> map = new HashMap<>();
        map.put("text",123);
        rabbitMqUtil.sendMsg("gdd","gddkey",map);
    }
    @RequestMapping("/test2")
    public Result send(){
        List<Student>studentList = studentService.list();
        List<StationMessage> stationMessageList = new ArrayList<>();
        studentList.forEach(v->{
            StationMessage stationMessage = new StationMessage();
            stationMessage.setStudentid(v.getStudentid());
            stationMessage.setTitle("dsds");
            stationMessage.setContent("dsds");
            stationMessage.setTimeer(LocalDateTime.now());
            stationMessage.setState(0);
            stationMessage.setTemplate(0);
            stationMessage.setUid("fd");
            stationMessage.setResult(0);
            stationMessage.setAuid(00);
            stationMessage.setExpirationTime(LocalDate.now());
            stationMessageList.add(stationMessage);
        });
        sendMessage2(stationMessageList);
        return Result.success(200,"ds",0);
    }
    public void sendMessage2(List<StationMessage> messages) {
        int batchSize = 2; // 指定批次大小

        IntStream.range(0, (messages.size() + batchSize - 1) / batchSize)
                .mapToObj(i -> messages.subList(i * batchSize, Math.min((i + 1) * batchSize, messages.size())))
                .map(batch -> batch.stream().map(JSONUtil::toJsonStr).collect(Collectors.toList()))
                .forEach(batch -> {
                    // 将每个小批次转换成字符串后发送给消息队列
                    String message = JSONUtil.toJsonStr(batch); // 这里假设你已经创建了一个Gson对象
                    rabbitMqUtil.sendMsg("gdd", "gddkey", message);
                });

    }



}
